import {defineStore} from "pinia";
import {computed, ref} from "vue";


export const userStore = defineStore('user', () => {
    const token = ref(localStorage.getItem('token') ?? '')
    const expire = ref(localStorage.getItem('expireTime') ?? '')
    const userLoginInfo = ref(JSON.parse(localStorage.getItem('userLoginInfo')??'{}'))

    const isLogin = computed(() => {
        let status = false
        if (token.value) {
            if (new Date().getTime()<parseInt(expire.value)){
                status = true
            }
        }
        return status
    })

    const getToken = ()=>{
        return token.value
    }

    const getLoginUserInfo=()=>{
        return userLoginInfo.value
    }

    actions:{

    }
    const setLoginUser=(jwt,expireTime,userLoginInfoVo)=>{
        token.value = jwt
        expire.value = expireTime
        userLoginInfo.value = JSON.stringify(userLoginInfoVo)
        localStorage.setItem('token',jwt)
        localStorage.setItem('expireTime',expireTime)
        localStorage.setItem('userLoginInfo',JSON.stringify(userLoginInfoVo))
    }

    return{
        isLogin,
        getToken,
        getLoginUserInfo,
        setLoginUser
    }

})
